﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApFLEXAttachFrameValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Flexibles;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands;

public class CApFLEXAttachFrameValidator : ITxValidator
{
  private bool m_showError;
  private bool m_validateEmptyName;
  private CApCableScopeStatus m_cableScopeStatus;

  internal CApFLEXAttachFrameValidator(CApCableScopeStatus scopeStatus)
  {
    this.CableScopeStatus = scopeStatus;
    this.ValidateEmptyName = true;
    this.m_showError = true;
  }

  internal bool ShowError
  {
    get => this.m_showError;
    set => this.m_showError = value;
  }

  internal bool ValidateEmptyName
  {
    get => this.m_validateEmptyName;
    set => this.m_validateEmptyName = value;
  }

  private CApCableScopeStatus CableScopeStatus
  {
    get => this.m_cableScopeStatus;
    set
    {
      if (value != null)
        this.m_cableScopeStatus = value;
      else
        this.m_cableScopeStatus = new CApCableScopeStatus();
    }
  }

  public ITxObject GetObject(string text)
  {
    ITxObject itxObject1 = (ITxObject) null;
    if (text != null && text.Length > 0)
    {
      TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
      if (objectsByName != null && ((Collection<ITxObject>) objectsByName).Count > 0)
      {
        string errorMessage = (string) null;
        foreach (ITxObject itxObject2 in (Collection<ITxObject>) objectsByName)
        {
          if (this.IsValidObject(itxObject2, out errorMessage))
          {
            itxObject1 = itxObject2;
            break;
          }
        }
      }
    }
    return itxObject1;
  }

  public string GetText(ITxObject obj)
  {
    string text = (string) null;
    try
    {
      text = obj.Name;
    }
    catch (Exception ex)
    {
    }
    return text;
  }

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    bool flag = false;
    errorMessage = (string) null;
    if (obj != null)
    {
      if (this.CableScopeStatus.IsCableInEquipment && !CApCableUtility.IsObjectInsideEquipment(obj, this.CableScopeStatus.ParentEquipment))
        errorMessage = string.Format(CApFLEXStringTable.AttachedObjectDoesNotBelongToCEQMessage, (object) obj.Name, (object) this.CableScopeStatus.ParentEquipment.Name);
      else
        flag = true;
    }
    else
      errorMessage = CApFLEXStringTable.AttachedObjectIsInvalidMessage;
    return flag;
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    errorMessage = (string) null;
    bool flag = false;
    if (text == "")
      flag = true;
    else if (this.GetObject(text) != null)
      flag = true;
    else
      errorMessage = !this.DoesExistObjectByName(text) ? CApFLEXStringTable.InvalidFrameNameMessage : string.Format(CApFLEXStringTable.InvalidFrame, (object) text);
    return flag;
  }

  private bool DoesExistObjectByName(string text)
  {
    bool flag = false;
    if (text != null && text.Length > 0)
    {
      TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
      flag = objectsByName != null && ((Collection<ITxObject>) objectsByName).Count > 0;
    }
    return flag;
  }

  private string InDoubleQuotes(string text) => $"\"{text}\"";
}
